<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
	public function index() {
            if(!$this->session->userdata('isLoggedIn')) {
                $data['scripts'] = array('login');
                $this->load->view('login/index',$data);                 
            }
            else {
                redirect('portal');
            }
           
	}

        public function ajaxValidate() {
            $email_address = $this->input->post('email_address');
            $password = $this->input->post('password');
            $passwordMD5 = md5($password);
            
            if(!$email_address || !$password) {
                $data['error'] = 'Please fill in all required fields.';
                echo json_encode($data);
            }
            else {
                
                $person = new Person();
                $person->get_by_email_address($email_address);
                
                if($person->id) {
                    if($passwordMD5 == $person->password) {
                        $this->session->set_userdata('isLoggedIn', TRUE);
                        $this->session->set_userdata('userName',$person->name.' '.$person->last_name);
                        $this->session->set_userdata('name',$person->name);
                        $this->session->set_userdata('last_name',$person->last_name);
                        $this->session->set_userdata('email_address',$person->email_address);
                        $this->session->set_userdata('job_description',$person->job_description);
                        $this->session->set_userdata('phone_number', $person->phone_number);
                        $this->session->set_userdata('address', $person->address);
                        $this->session->set_userdata('postalcode', $person->postalcode);
                        $this->session->set_userdata('city', $person->city);
                        $this->session->set_userdata('extra', $person->extra);
                        $this->session->set_userdata('facebook', $person->facebook);
                        $this->session->set_userdata('twitter', $person->twitter);
                        $this->session->set_userdata('linkedin', $person->linkedin);
                        $this->session->set_userdata('other', $person->other);
                       
                        $data['redirect'] = base_url().'index.php/portal';
                        echo json_encode($data);
                    }
                    else {
                        $data['error'] = 'Incorrect password.';
                        echo json_encode($data);
                    }
                }
                else {
                    $data['error'] = 'Person does not exist.';
                    echo json_encode($data);   
                }
            }
        }
        
        public function destroy() { 
            $this->session->sess_destroy();
            redirect('login');
        }
}